﻿/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：Default.aspx.cs
//
//文件功能描述：显示最新订单
// 
//创建标识：2007.11.19 李鹏
//创建描述：
// 
//修改标识：2007.11.28李鹏
//修改描述：修改异常捕捉
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost;


public partial class UserControls_Default : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DocumentService documentService = new DocumentService();
           
            //
            //配置分页参数
            //
            int iPageSize = 10;
            int iPageIndex = 1;

            //
            //绑定第一页的数据
            //
            DataSet ds = documentService.GetLastOrderList(iPageSize, iPageIndex);
            GridviewBind(ds);
           
            //
            //隐藏控件保存共有多少条记录
            //
            int iOrders = documentService.GetOrderListRows(0, 0, 0, String.Empty, String.Empty);
            hdfRecords.Value = iOrders.ToString();
            
            //
            //绑定分页按钮
            //
            BindToPaging(iPageSize, iPageIndex);
        }
    }

    #region //绑定分页按钮
    void BindToPaging(int iPageSize, int iPageIndex)
    {
        DocumentService documentService = new DocumentService();
        //
        //读取订单数
        //
        int iOrders = Convert.ToInt32(hdfRecords.Value.Trim());

        if (iOrders > 0)
        {
            //
            //有订单
            //

            //
            //订单数
            //
            lblRecordCount.Text = "共 " + iOrders.ToString() + " 条";
            lblPageCount.Visible = true;
            lblNoRecord.Visible = false;

            //
            //计算总页数
            //
            int iPages = iOrders / iPageSize;
            if (iOrders % iPageSize != 0)
            {
                iPages += 1;
            }//if(iOrders % iPageSize != 0)
            lblPageCount.Text = "共 " + iPages.ToString() + " 页";
            lblPageCount.Visible = true;

            //
            //有多页
            //
            if (iPages > 1)
            {
                btnFirst.Visible = true;
                btnPrev.Visible = true;
                btnNext.Visible = true;
                btnLast.Visible = true;
            }
            else
            {
                btnFirst.Visible = false;
                btnPrev.Visible = false;
                btnNext.Visible = false;
                btnLast.Visible = false;
            }//if (iPages > 1)

            //
            //当前页
            //
            lblCurrentIndex.Text = "第 " + iPageIndex + " 页";
            lblCurrentIndex.Visible = true;
        }
        else
        {
            //
            //没有订单
            //
            lblNoRecord.Visible = true;
            lblPageCount.Visible = false;
            lblRecordCount.Visible = false;
            lblCurrentIndex.Visible = false;

            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;

        }//if(iOrders > 0)
    }

    #endregion

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="ds"></param>
    private void GridviewBind(DataSet ds)
    {
        //
        //绑定数据
        //
        this.gdvNewOrders.DataSource = ds.Tables[0].DefaultView;
        this.gdvNewOrders.DataBind();
    }

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        //
        //获取当前页
        //
        string strPageIndex = lblCurrentIndex.Text.Trim();
        strPageIndex = strPageIndex.Substring(2);
        strPageIndex = strPageIndex.Substring(0, strPageIndex.Length - strPageIndex.LastIndexOf(" "));
        int iPageIndex = Convert.ToInt32(strPageIndex);

        //
        //获取总页数
        //
        string strPageCount = lblPageCount.Text.Trim();
        strPageCount = strPageCount.Substring(2);
        strPageCount = strPageCount.Substring(0, strPageCount.Length - strPageCount.LastIndexOf(" "));
        int iPageCount = Convert.ToInt32(strPageCount);

        //
        //判断操作类型
        //
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "prev"://上一页
                if (iPageIndex > 1)
                {
                    iPageIndex -= 1;
                }
                break;
            case "next"://下一页
                if (iPageIndex < iPageCount)
                {
                    iPageIndex += 1;
                }
                break;
            case "last"://最后一页
                iPageIndex = iPageCount;
                break;
            default://首页
                iPageIndex = 1;
                break;
        }//switch (arg)

        //
        //配置查询条件
        //
        int iPageSize = 10;

        //
        //绑定下一页的数据
        //
        DataSet ds = (new DocumentService()).GetLastOrderList(iPageSize, iPageIndex);
        GridviewBind(ds);

        //
        //绑定分页按钮
        //
        BindToPaging(iPageSize, iPageIndex);
    }
}
